package com.mbyte.easy.rest.scraprecord;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.scraprecord.entity.Scraprecord;
import com.mbyte.easy.scraprecord.service.IScraprecordService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@RestController
@RequestMapping("rest/scraprecord")
public class RestScraprecordController extends BaseController  {

    @Autowired
    private IScraprecordService scraprecordService;

    /**
    * 查询列表
    *
    * @param pageNo
    * @param pageSize
    * @param scraprecord
    * @return
    */
    @RequestMapping
    public AjaxResult index(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String scrapApplytimeSpace, String scrapCreatetimeSpace, Scraprecord scraprecord) {
        Page<Scraprecord> page = new Page<Scraprecord>(pageNo, pageSize);
        QueryWrapper<Scraprecord> queryWrapper = new QueryWrapper<Scraprecord>();

        if(scraprecord.getScrapapplyId() != null  && !"".equals(scraprecord.getScrapapplyId() + "")) {
            queryWrapper = queryWrapper.like("scrapapply_id",scraprecord.getScrapapplyId());
         }


        if(scraprecord.getRecordId() != null  && !"".equals(scraprecord.getRecordId() + "")) {
            queryWrapper = queryWrapper.like("record_id",scraprecord.getRecordId());
         }


        if(scraprecord.getEquId() != null  && !"".equals(scraprecord.getEquId() + "")) {
            queryWrapper = queryWrapper.like("equ_id",scraprecord.getEquId());
         }


        if(scraprecord.getEquName() != null  && !"".equals(scraprecord.getEquName() + "")) {
            queryWrapper = queryWrapper.like("equ_name",scraprecord.getEquName());
         }


        if(scraprecord.getEquStatus() != null  && !"".equals(scraprecord.getEquStatus() + "")) {
            queryWrapper = queryWrapper.like("equ_status",scraprecord.getEquStatus());
         }


        if(scraprecord.getEquType() != null  && !"".equals(scraprecord.getEquType() + "")) {
            queryWrapper = queryWrapper.like("equ_type",scraprecord.getEquType());
         }


        if(scraprecord.getScrapProson() != null  && !"".equals(scraprecord.getScrapProson() + "")) {
            queryWrapper = queryWrapper.like("scrap_proson",scraprecord.getScrapProson());
         }


        if(scraprecord.getScrapApplytime() != null  && !"".equals(scraprecord.getScrapApplytime() + "")) {
            queryWrapper = queryWrapper.like("scrap_applytime",scraprecord.getScrapApplytime());
         }


        if(scraprecord.getApprover() != null  && !"".equals(scraprecord.getApprover() + "")) {
            queryWrapper = queryWrapper.like("approver",scraprecord.getApprover());
         }


        if(scraprecord.getScrapReason() != null  && !"".equals(scraprecord.getScrapReason() + "")) {
            queryWrapper = queryWrapper.like("scrap_reason",scraprecord.getScrapReason());
         }


        if(scraprecord.getScrapCreatetime() != null  && !"".equals(scraprecord.getScrapCreatetime() + "")) {
            queryWrapper = queryWrapper.like("scrap_createtime",scraprecord.getScrapCreatetime());
         }


        if(scraprecord.getScrapRemake() != null  && !"".equals(scraprecord.getScrapRemake() + "")) {
            queryWrapper = queryWrapper.like("scrap_remake",scraprecord.getScrapRemake());
         }

        IPage<Scraprecord> pageInfo = scraprecordService.page(page, queryWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("searchInfo",  scraprecord);
        map.put("pageInfo", new PageInfo(pageInfo));

        return this.success(map);
    }


    /**
    * 添加
    * @param scraprecord
    * @return
    */
    @PostMapping("add")
    public AjaxResult add(Scraprecord scraprecord){
        return toAjax(scraprecordService.save(scraprecord));
    }

    /**
    * 添加
    * @param scraprecord
    * @return
    */
    @PostMapping("edit")
    public AjaxResult edit(Scraprecord scraprecord){
        return toAjax(scraprecordService.updateById(scraprecord));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(scraprecordService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(scraprecordService.removeByIds(ids));
    }

}

